package com.yanwq.sayhi.controller.club.activity;

import android.os.Bundle;
import android.view.Menu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yanwq.pulltorefreshview.view.PullToLoadListener;
import com.yanwq.pulltorefreshview.view.PullToRefreshLayout;
import com.yanwq.pulltorefreshview.view.PullToRefreshListView;
import com.yanwq.pulltorefreshview.view.PullToRefreshListener;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.OkJsonArrayResponseCallback;
import com.yanwq.sayhi.common.client.OkRequestParams;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.controller.club.adapter.ClubVipAdapter;
import com.yanwq.sayhi.dao.bean.Club;
import com.yanwq.sayhi.dao.bean.ClubVip;

import java.util.List;

import butterknife.BindView;

public class ClubVipListActivity extends BaseToolbarActivity {
    @BindView(R.id.vip_refresh)
    PullToRefreshLayout refreshLayout;
    @BindView(R.id.vip_list)
    PullToRefreshListView listView;

    private ClubVipAdapter adapter;
    private Club mClub;
    private int mPage;
    private int mSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_vip_list);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_back, menu);
        return true;
    }

    private void autoRefresh() {
        post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.startRefresh();
            }
        });
    }

    @Override
    public void init() {
        mClub = (Club) getIntent().getSerializableExtra("club");
        mPage = 0;
        mSize = 20;
        adapter = new ClubVipAdapter();
    }

    @Override
    public void initView() {
        mToolbar.setTitle(String.format("%s 的成员", mClub.getName()));

        listView.setAdapter(adapter);

        refreshLayout.setPullToRefreshListener(new PullToRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        refreshLayout.setPullToLoadListener(new PullToLoadListener() {
            @Override
            public void onLoad() {
                load();
            }
        });

        autoRefresh();
    }

    private void refresh() {
        mPage = 0;
        OkRequestParams params = new OkRequestParams();
        params.put("clubId", mClub.getId());
        params.put("page", mPage);
        params.put("size", mSize);
        OkRestClient.getInstance().get(this, "/clubVip/list/club", params, new OkJsonArrayResponseCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(JSONArray jsonArray) {
                List<ClubVip> vips = JSON.parseArray(jsonArray.toJSONString(), ClubVip.class);
                adapter.refresh(vips);
            }

            @Override
            public void onStop() {
                refreshLayout.stopRefresh();
            }
        });
    }

    private void load() {
        mPage++;
        OkRequestParams params = new OkRequestParams();
        params.put("clubId", mClub.getId());
        params.put("page", mPage);
        params.put("size", mSize);
        OkRestClient.getInstance().get(this, "/clubVip/list/club", params, new OkJsonArrayResponseCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(JSONArray jsonArray) {
                List<ClubVip> vips = JSON.parseArray(jsonArray.toJSONString(), ClubVip.class);
                adapter.load(vips);
            }

            @Override
            public void onStop() {
                refreshLayout.stopLoad();
            }
        });
    }
}
